package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.BuilderRepairReportDTO;
import com.eastfair.home.dto.BuilderRepairReportPageQuery;
import com.eastfair.home.dto.BuilderRepairReportSaveDTO;
import com.eastfair.home.dto.BuilderRepairReportUpdateDTO;
import com.eastfair.home.entity.BuilderRepairReport;
import com.eastfair.home.service.BuilderRepairReportService;
import com.eastfair.home.vo.BuilderRepairReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 搭建商报修申请
 * </p>
 *
 * @author dqq
 * @date 2022-11-24
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/builderRepairReport")
@Api(value = "BuilderRepairReport", tags = "搭建商报修申请")
@PreAuth(replace = "home:builderRepairReport:")
public class BuilderRepairReportController extends SuperController<BuilderRepairReportService, Long, BuilderRepairReport, BuilderRepairReportPageQuery, BuilderRepairReportSaveDTO, BuilderRepairReportUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private BuilderRepairReportService builderRepairReportService;

    @Override
    public void handlerResult(IPage<BuilderRepairReport> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<BuilderRepairReport> builderRepairReportList = list
                .stream()
                .map((map) -> BuilderRepairReport.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(builderRepairReportList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BuilderRepairReportPageQuery> params) {
        BuilderRepairReportPageQuery pageQuery = params.getModel();
    }

    /**
     * 查询搭建商报修申请列表VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("查询搭建商报修申请列表VO")
    @PostMapping("/query/builderRepairReport")
    public R<Page<BuilderRepairReportVO>> queryBuilderRepairReport(@RequestBody PageParams<BuilderRepairReportDTO> pageParams) {
        log.info("queryBuilderRepairReport - 查询搭建商报修申请列表VO, pageParams={}", pageParams);
        try {
            Page<BuilderRepairReportVO> page = builderRepairReportService.listBuilderRepairReportVO(pageParams);
            return R.successDef(page);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 查询搭建商报修申请列表VO
     *
     * @param query the query
     * @return the r
     */
    @PostMapping("/query/builderRepairReport/list")
    public R<List<BuilderRepairReportVO>> queryBuilderRepairReportList(@RequestBody BuilderRepairReportDTO query) {
        log.info("queryBuilderRepairReport - 查询搭建商报修申请列表VO, query={}", query);
        try {
            List<BuilderRepairReportVO> page = builderRepairReportService.listBuilderRepairReportVO(query);
            return R.successDef(page);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 报修单号获取搭建报修申请VO.
     *
     * @param repairReportNumber the repair report number
     * @return the by report repair number
     */
    @ApiOperation("报修单号获取搭建报修申请VO")
    @GetMapping("/get/{repairReportNumber}")
    public R<BuilderRepairReportVO> getByReportRepairNumber(@PathVariable("repairReportNumber") String repairReportNumber) {
        log.info("getByReportRepairNumber - 报修单号获取搭建报修申请VO, repairReportNumber={}", repairReportNumber);
        try {
            BuilderRepairReportVO builderRepairReportVO = builderRepairReportService.getVOByReportRepairNumber(repairReportNumber);
            return R.successDef(builderRepairReportVO);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 保存搭建商报修申请.
     *
     * @param builderRepairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("保存搭建商报修申请")
    @SysLog("添加报修申请")
    @PostMapping("/save")
    public R<BuilderRepairReport> saveBuilderRepairReport(@RequestBody BuilderRepairReportDTO builderRepairReportDTO) {
        log.info("saveBuilderRepairReport - 保存搭建商报修申请, repairReportDTO={}", builderRepairReportDTO);
        try {
            return R.successDef(builderRepairReportService.saveBuilderRepairReport(builderRepairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新搭建商报修申请.
     *
     * @param builderRepairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("更新搭建商报修申请")
    @SysLog("修改报修申请")
    @PostMapping("/update")
    public R<BuilderRepairReport> updateBuilderRepairReport(@RequestBody BuilderRepairReportDTO builderRepairReportDTO) {
        log.info("updateBuilderRepairReport - 更新搭建商报修申请, repairReportDTO={}", builderRepairReportDTO);
        try {
            return R.successDef(builderRepairReportService.updateBuilderRepairReport(builderRepairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 取消搭建商报修申请.
     *
     * @param repairReportNumber the repair report number
     * @return the r
     */
    @ApiOperation("取消搭建商报修申请")
    @SysLog("取消报修申请")
    @GetMapping("/cancel/{repairReportNumber}")
    public R<Boolean> cancelBuilderRepairReport(@PathVariable("repairReportNumber") String repairReportNumber) {
        log.info("cancelRepairReport - 取消搭建商报修申请={}", repairReportNumber);
        try {
            return R.successDef(builderRepairReportService.cancelBuilderRepairReport(repairReportNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新搭建商报修申请是否预定了服务.
     *
     * @param builderRepairReportDTO the builder repair report dto
     * @return the r
     */
    @PostMapping("/update/isBookService")
    public R<Boolean> updateIsBookService(@RequestBody BuilderRepairReportDTO builderRepairReportDTO) {
        log.info("updateIsBookService - 更新搭建商报修申请是否预定了服务, builderRepairReportDTO={}", builderRepairReportDTO);
        try {
            return R.successDef(builderRepairReportService.updateIsBookService(builderRepairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新搭建商报修申请需赔付场馆金额.
     *
     * @param builderRepairReportDTO the builder repair report dto
     * @return the r
     */
    @PostMapping("/update/compensateVenueAmount")
    public R<Boolean> updateCompensateVenueAmount(@RequestBody BuilderRepairReportDTO builderRepairReportDTO) {
        log.info("updateIsBookService - 更新搭建商报修申请需赔付场馆金额, builderRepairReportDTO={}", builderRepairReportDTO);
        try {
            return R.successDef(builderRepairReportService.updateCompensateVenueAmount(builderRepairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新搭建商报修申请检测结果.
     *
     * @param builderRepairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("更新搭建商报修申请检测结果")
    @SysLog("修改报修检测结果")
    @PostMapping("/update/checkResult")
    public R<Boolean> updateCheckResult(@RequestBody BuilderRepairReportDTO builderRepairReportDTO) {
        log.info("updateCheckResult - 更新搭建商报修申请检测结果, repairReportDTO={}", builderRepairReportDTO);
        return R.successDef(builderRepairReportService.updateCheckResult(builderRepairReportDTO));
    }

    /**
     * 搭建商报修申请报馆.
     *
     * @param repairReportNumber the repair report number
     * @return the r
     */
    @ApiOperation("搭建商报修申请报馆")
    @SysLog("报修申请报馆")
    @GetMapping("/reportVenue/{repairReportNumber}")
    public R<Boolean> reportVenueRepairReport(@PathVariable("repairReportNumber") String repairReportNumber) {
        log.info("reportVenueRepairReport - 搭建商报修申请报馆, repairReportNumber={}", repairReportNumber);
        return R.successDef(builderRepairReportService.updateReportVenue(repairReportNumber));
    }

    /**
     * 更新搭建商报修申请为已完成.
     *
     * @param builderRepairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("更新搭建商报修申请为已完成")
    @SysLog("修改申请报馆完成")
    @PostMapping("/complete")
    public R<Boolean> completeRepairReport(@RequestBody BuilderRepairReportDTO builderRepairReportDTO) {
        log.info("completeRepairReport - 更新搭建商报修申请为已完成, repairReportDTO={}", builderRepairReportDTO);
        try {
            return R.successDef(builderRepairReportService.updateComplete(builderRepairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

}
